$(function(){
	parent.IndexPage.toggleLocker('close'); // 沉浸式体验
	// 让jquery的contains方法不区分大小写的识别
	$.expr[":"].contains = $.expr.createPseudo(function(arg) {
	    return function( elem ) {
	        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
	    };
	});
});
var BlogPage = {
	'lastBlogType':null,
	// 查询博客分类成功回调
	'findCategorySucc':function(data,status,xhr) {
		// 输出博客分类
		
		// 搜索文章
		$("#searchArticle").bind('input propertychange',function(){
			var $articleList = $(".t-article-list");
			var val = $(this).val();
			if(StringUtil.isBlank(val)){
				$articleList.find(".t-article").css("display","block");
			}else {
				$articleList.find(".t-article").css("display","none");
				$articleList.find(".t-article a:contains(" + val + ")").each(function(){
					$(this).closest(".t-article").css("display","block");
				});
			}
		});
		// 搜索博客分类
		$("#searchBlogType").bind('input propertychange',function(){
			var val = $(this).val();
			categoryTree.search(val);
		});
	},
	'onSelected':function(node) {
		var value = node['codeInfoValue'];
		BlogPage.lastBlogType = value;
		findBlogByCategoryAjax.tempUrl(function(url) {
			return url + "/" + value;
		});
		findBlogByCategoryAjax.send();
	},
	'uploadArticle':function(){
		var node = categoryTree.getCurrentNode();
		MsgBox.open("blogAdd", "上传博客", ctx + "/datum/blog/blogAdd/index/" + node.codeInfoValue, '570px');
	},
	// 刷新当前博客页面
	'refreshBlogPage':function() {
		findBlogByCategoryAjax.tempUrl(function(url) {
			return url + "/" + BlogPage.lastBlogType;
		}).send();
	},
	// 刷新阅读次数
	'refreshReadCount':function(data,status,xhr) {
		var $readCount = $(".t-article[data-id='" + data.info + "']").find("[data-tag='visits'] .t-note-value");
		var count = parseInt($readCount.text()) + 1;
		$readCount.text(count); 
	},
	// 打开编辑博客类型对话框
	'openEditBlogTypeDialog':function() {
//		MsgBox.open("editBlogType", "修改博客类型", ctx + "/datum/blog/editBlogType/index", '650px');
		MsgBox.open("editBlogType", "修改博客类型", ctx + "/datum/blog/editTreeBlogType/index", '650px');
	},
	// 根据分类查询博客成功回调
	'findBlogByCategorySucc':function(data, status, xhr) {
		$(".t-article-list").article({
			'data':data,
			'handleData':function(data) {
				var list = data.info;
				$.each(list,function(index,item){
					var fileName = item.fileInfo.fileName;
					if(fileName.startsWith("http://") || fileName.startsWith("https://") 
							|| fileName.startsWith("ftp://")){
						item["target"] = fileName;
					} else {
						item["target"]="http://"+serverName+":8080/datum/"+fileName;
					}
				});
				return data;
			},
			'contentDefine':{ // 定义内容结构
				"title":"blogName",
				"target":"target", // 目标网页，超链接
				"summary":"remarks", // 内容概述 
				"id":"blogId",// 指定id
				"notes":[
				     {"name":"更新时间","key":"createTime"},
				     {"name":"作者","key":"author"},
				     {"name":"阅读次数","key":"visits"}
				 ] // 脚注 
			},
			'onItemClick':function(e,item) {
				signReadAjax.tempUrl(function(url){
					return url + "/" + item.blogId;
				});
				signReadAjax.send();
			},
			'optBar':"<i class='fa fa-fw fa-trash' title='删除' onclick='BlogPage.deleteBlog(this)'></i>" +
					"<i class='fa fa-fw fa-edit' title='修改' onclick='BlogPage.updateBlog(this)'></i>"
		});
	},
	// 删除博客
	'deleteBlog':function(ele) {
		MsgBox.confirm("确定删除此博客吗？", null, function() {
			var id = $(ele).closest(".t-article").attr("data-id");
			deleteBlogByIdAjax.tempUrl(function(url) {
				return url + "/" + id;
			});
			deleteBlogByIdAjax.send();
		});
	},
	// 删除博客成功
	'deleteBlogSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		BlogPage.refreshBlogPage();
	},
	// 修改博客
	'updateBlog':function(ele) {
		var id = $(ele).closest(".t-article").attr("data-id");
		MsgBox.open("updateBlogType", "修改博客信息", ctx + "/datum/blog/blogUpdate/index/" + id, '570px');
	},
	// 修改成功
	'updateBlogSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		BlogPage.refreshBlogPage();
	}
};
